home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Tool Chest / Testing & Debugging / Mac OS Development Toolkit / Automation Essentials 2.3.0 / Host Automation Folder / SPEC Libs / Geometry.lib < prev    next >
Encoding:
Text File  |  1998-03-19  |  8.5 KB  |  226 lines  |  [TEXT/MPS ]

  1. #
  2. # ****************************************************************************
  3. #
  4. #    File Name:        Geometry.lib
  5. #
  6. #    Contains:    xxx put contents here xxx
  7. #
  8. #    Written by:    KTA, KL, ML, GS et al
  9. #
  10. #    Copyright:    © 1993-1996 by Apple Computer, Inc., all rights reserved.
  11. #
  12. # ****************************************************************************
  13. #            C h a n g e        H i s t o r y (most recent first):
  14. # ****************************************************************************
  15. #
  16. #        Vers      Date        Author        Description
  17. #        ----    --------    ------    ---------------------------------------------
  18. #     <1.0.6>      2/8/96    JC        Changed all window .r (Rect) traits to .b (bound) traits and
  19. #                                    changed any window insets to reflect new bound rect.
  20. #     <1.0.5>     1/19/95    KTA        Changed the name of ExceptionHandler() to ExceptionDispatcher().
  21. #     <1.0.4>    11/29/94    ML        Added Exception Handling support
  22. #     <1.0.3>    12/13/93    KTA        Added task CenterPointOfRect()
  23. #        <1+>     5/21/93    NAGA        Adding header and porting old files to follow new standards
  24. #
  25. # ****************************************************************************
  26.  
  27. ########################################################################
  28. #                            External libraries 
  29. #=======================================================================
  30. Libraries "ExceptionHandling.Lib";
  31.  
  32. #########################################################################
  33. #                        PtInRect(thePoint,theRect)
  34. #========================================================================
  35. # Author:            NJV
  36. # Description:        This routine determines whether or not thePoint 
  37. #                    is contained within theRect (including borders). 
  38. # Parameters:        thePoint - a list of two integers
  39. #                    theRect - holds the coordinates of the rectangle.
  40. # Returns:            true - point lies in the rectangle.
  41. #                    false - point does NOT lie in the rectangle.
  42. # Examples:            isIt := PtInRect(thePoint,theRect).
  43. # Assumptions:        None
  44. #========================================================================
  45. # History:
  46. #
  47. #########################################################################
  48. TASK PtInRect(thePoint,theRect) begin
  49.     return( ((thePoint[1] >= theRect[1]) and (thePoint[1] <= theRect[3]))
  50.             and ((thePoint[2] >= theRect[2]) and (thePoint[2] <= theRect[4])) );
  51. end;
  52.  
  53. ########################################################################
  54. #                        RndPtInWindow(specifier:=-1)
  55. #=======================================================================
  56. # Author:            NJV
  57. # Description:        Returns a random point inside of the window.  If specifier
  58. #                    is not passed, it will return a random point of the 
  59. #                    content region of the first window with a grow and a
  60. #                    zoom box, or if there are none, then the first window
  61. #                    it finds, and if no windows are present, returns 0.
  62. # Parameters:        specifier - title or ordinality of window
  63. # Returns:            0 - couldn't find window
  64. #                    Point -    list of 2 coordinates - {x,y}
  65. # Examples:            thePoint := RndPtInWindow();    # get random point in window content
  66. #                    thePoint := RndPtInWindow(1);    # random point in window ordinality 1
  67. #=======================================================================
  68. # History:
  69. #    ML    11/29/94    Added exception handling support
  70. #########################################################################
  71. TASK RndPtInWindow(specifier:= -1) begin
  72.     if (specifier = -1)
  73.         return(RndPtInRect());    # return point in first window w/ size and zoom box
  74.     
  75.     if (TypeOf(specifier) = 'integer')
  76.         there := _match ([window o:specifier],1);
  77.     else
  78.         there := _match ([window t:specifier],1);
  79.         
  80.     if (there)
  81.         return(RndPtInRect(there.b));    # return random point in window's bound rect
  82.     else
  83.         return(0);
  84. end;
  85.  
  86. ########################################################################
  87. #                            RndPtInRect(theRect)
  88. #=======================================================================
  89. # Author:            NJV
  90. # Description:        Returns a random point inside of theRect.  If theRect
  91. #                    is not passed, it will return a random point of the 
  92. #                    content region of the first window with a grow and a
  93. #                    zoom box, or if there are none, then the first window
  94. #                    it finds, and if no windows are present, returns 0.
  95. # Parameters:        theRect - bounding rectangle
  96. # Returns:            0 - Default behavior requested with no windows up
  97. #                    Point -    list of 2 coordinates - {x,y}
  98. # Examples:            thePoint := RndPtInRect();    # get random point in window content
  99. #                    thePoint := RndPtInRect({20,20,80,80});    # random point in {20,20,80,80}
  100. # Assumptions:        None
  101. #=======================================================================
  102. # History:
  103. #    ML    11/29/94    Added exception handling support
  104. #   JC   2/02/96    Changed add window .r traits to .b traits. 
  105. #########################################################################
  106. TASK RndPtInRect(theRect:=-1) begin
  107.     if (theRect = -1) begin    # use content rect of first window with grow and zoom box
  108.         foo := _match ([window g:true z:true],1);
  109.         if (foo)
  110.             theRect := foo.b;
  111.         else begin
  112.             foo := _match ([window],1);        # try to find any window
  113.             if (not foo)
  114.                 return(0);
  115.             else
  116.                 theRect := foo.b;
  117.         end;
  118.     end;
  119.     x := Random(theRect[1],theRect[3]);
  120.     y := Random(theRect[2],theRect[4]);
  121.     return({x,y});
  122. end;
  123.  
  124. #########################################################################
  125. #                        RectInRect(rect1,rect2)
  126. #========================================================================
  127. # Author:            SMQ
  128. # Description:        This routine determines whether or not first rectangle 
  129. #                    lies completely within the second.  This is done by 
  130. #                    comparing the coordinates of both rectangles. 
  131. # Parameters:        rect1 - holds the coordinates of the first rectangle.
  132. #                    rect2 - holds the coordinates of the second rectangle.
  133. # Returns:            value - first rectangle lies in the second.
  134. #                    false - first rectangle does NOT lie in the second.
  135. # Examples:            RectInRect(rect1,rect2).
  136. # Assumptions:        None
  137. #========================================================================
  138. # History:
  139. #
  140. #########################################################################
  141. TASK RectInRect(rect1, rect2) begin
  142.  
  143.     Left1 := rect1[1];
  144.     Top1 := rect1[2];
  145.     Right1 := rect1[3];
  146.     Bottom1 := rect1[4];
  147.     
  148.     Left2 := rect2[1];
  149.     Top2 := rect2[2];
  150.     Right2 := rect2[3];
  151.     Bottom2 := rect2[4];
  152.     
  153.     return (((Left2 <= Left1) and (Top2 <= Top1) and (Right2 >= Right1) and (Bottom2 >= Bottom1)));
  154. end;
  155.  
  156.  
  157. ########################################################################
  158. #                        GetXYRandom(inset, Rect, numPts)
  159. #=======================================================================
  160. # Author:             SL
  161. # Description:        Returns a random X and Y coordinate in the specified
  162. #                    Rect.
  163. # Parameters:        inset - ltrb inset from scrn or window
  164. #                    specifier > 0 window to find coords in
  165. #                              = O coords in window with s:doc g:true and c:true
  166. #                              < 0 coords in specified screen.  Screen numbersta
  167. #                                is negative of specifier.
  168. # Returns:            xyRandom - List of random X and Y.  eg. { x, y }
  169. #=======================================================================
  170. # History:
  171. #    ML    11/29/94    Added exception handling support
  172. ########################################################################
  173. TASK GetXYRandom(inset := { 0,0,0,0}, Rect := {}, numPts := 1) 
  174. begin        
  175.     pointlist := {};
  176.     if not (Rect) 
  177.         try match[screen m:1 r:?Rect];
  178.         catch theError
  179.             ExceptionDispatcher(theError);
  180.     if (rect) 
  181.     begin
  182.         maxX := rect[3] - rect[1] - inset[3];
  183.         maxY := rect[4] - rect[2] - inset[4];
  184.         for i := 1 to numPts
  185.         begin
  186.             xRandom := random(inset[1], maxX);
  187.             yRandom := random(inset[2], maxY);
  188.             
  189.             thePoint := { xRandom, yRandom };
  190.             if(numPts > 1)        # Do we want a point list
  191.                 pointList := insert(thePoint , i, pointList );
  192.         end;
  193.         if(numPts = 1)    # If there is only one return it just like we always have
  194.             returnVal := thePoint;
  195.         else             # Otherwise return a list of points { {xPt,yPt},{xPt2, yPt2} };
  196.             returnVal := pointList;
  197.     end;
  198.     else
  199.         returnVal := 0;
  200.     return(returnVal);
  201. end; # getXYRandom()
  202.  
  203.  
  204. ########################################################################
  205. #                        CenterPointOfRect(pTheRect)
  206. #=======================================================================
  207. # Author:             KTA
  208. # Description:        Returns the center point of the input parameter <pTheRect>
  209. # Parameters:        pTheRect - the rect that you want the center point of.
  210. # Returns:            returnVal  - List { x, y }
  211. #=======================================================================
  212. # History:
  213. # KTA    12/13/93    Created
  214. ########################################################################
  215. TASK CenterPointOfRect(pTheRect := {})
  216. begin
  217.     returnVal := 0;
  218.     if(pTheRect)
  219.     begin
  220.         x := ((pTheRect[3] + pTheRect[1])/2);
  221.         y := ((pTheRect[4] + pTheRect[2])/2);
  222.         
  223.         returnVal := {x,y};
  224.     end;
  225.     return(returnVal);
  226. end;